package com.upcode.weighing.admin.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtils {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 向redis里存入数据和设置缓存时间
     * @param key   设置key
     * @param value 设置 value
     * @param seconds 单位是秒
     */
    public void set(String key,String value,Long seconds){
        stringRedisTemplate.opsForValue().set(key, value,seconds,TimeUnit.SECONDS);
    }

    /**
     *  根据key删除缓存
     * @param key
     */
    public Boolean delete(String key){
        return stringRedisTemplate.delete(key);
    }

    /**
     *  检查key是否存在，返回boolean值
     * @param key
     */
    public Boolean hasKey(String key){
        return stringRedisTemplate.hasKey(key);
    }

    /**
     *  根据key获取过期时间
     * @param key
     */
    public Long getExpire(String key){
        return stringRedisTemplate.getExpire(key);
    }

}
